#!/usr/bin/python3
# -*- coding: utf-8 -*-
# ======================================#
# @Author   : duanqizhong
# @time     : 2020/6/21 9:22
# @file     : api_decorate.py
# @Software : PyCharm
# ======================================#
from functools import wraps

from flask import request, jsonify, session

from apps import log


def decorate_update(view):
    @wraps(view)
    def wrapper(*args, **kwargs):
        # if int(session.get("role")) not in (2, 3, 9999):
        #     infos = {
        #         "status": False,
        #         "msg": "no privileges operator update!"
        #     }
        #     return jsonify(infos)

        infos = {
            "status": True,
            "msg": "ok"
        }
        if request.content_type is None:
            infos["status"] = False
            infos["msg"] = f"please set header content_type: application/json, current is: {request.content_type}"
        elif not request.content_type.startswith('application/json'):
            infos["status"] = False
            infos["msg"] = f"please set header content_type: application/json, current is: {request.content_type}"
        elif request.get_json().get("id", None) is None:
            infos["status"] = False
            infos["msg"] = f"update table data, id is required!"
        log.info(infos)
        if not infos["status"]:
            return jsonify(infos)

        return view(kwargs.get("table"))

    return wrapper


def decorate_insert(view):
    @wraps(view)
    def wrapper(*args, **kwargs):
        # if int(session.get("role")) not in (1, 2, 3, 9999):
        #     infos = {
        #         "status": False,
        #         "msg": "no privileges operator insert!"
        #     }
        #     return jsonify(infos)

        infos = {
            "status": True,
            "msg": "ok"
        }
        if request.content_type is None or not request.content_type.startswith('application/json'):
            infos["status"] = False
            infos["msg"] = f"please set header content_type: application/json, current is: {request.content_type}"
            log.warning(infos)
            return jsonify(infos)

        return view(kwargs.get("table"))

    return wrapper


def decorate_query(view):
    @wraps(view)
    def wrapper(*args, **kwargs):
        infos = {
            "status": True,
            "msg": "ok"
        }

        if request.content_type is not None:
            if not request.content_type.startswith('application/json'):
                infos["status"] = False
                infos[
                    "msg"] = f"please set header content_type: application/json or None, current is: {request.content_type}"
                log.warning(infos)
                return jsonify(infos)
        return view(kwargs.get("table"))

    return wrapper


def decorate_delete(view):
    @wraps(view)
    def wrapper(*args, **kwargs):
        # if int(session.get("role")) not in (3, 9999):
        #     infos = {
        #         "status": False,
        #         "msg": "no privileges operator delete!"
        #     }
        #     return jsonify(infos)
        infos = {
            "status": True,
            "msg": "ok"
        }
        if request.content_type is None:
            if kwargs.get("num", None) is None:
                infos["status"] = False
                infos["msg"] = "id is required!"
        elif request.content_type.startswith('application/json'):
            if kwargs.get("num", None) is None:
                infos["status"] = False
                infos["msg"] = "id is required!"
        else:
            infos[
                "msg"] = f"please set header content_type: application/json or None, current is: {request.content_type}"
        log.info(infos)
        if not infos["status"]:
            return infos

        return view(kwargs.get("table"), kwargs.get("num"))

    return wrapper


def decorate_session(view):
    @wraps(view)
    def wrapper(*args, **kwargs):
        if session.get("username"):
            return view(args, kwargs)
        else:
            info = {
                "status": False,
                "data": None,
                "msg": "no login!"
            }

            return jsonify(info)

    return wrapper
